package com.eascs.web.p2p.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.p2p.common.entity.AdminUserEntity;
import com.eascs.p2p.common.entity.InvestorEntity;
import com.eascs.p2p.common.entity.InvestorWithdrawRecordEntity;
import com.eascs.p2p.common.service.InvestorService;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.p2p.common.wallet.service.InvestorWalletService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;

import jodd.util.StringUtil;

@RestController
@RequestMapping("/withdrawRest")
public class WithdrawRestController extends BaseRestController {
	@Reference
	InvestorWalletService  investorWalletService;
	@Reference
	InvestorService investorService;
	/**
	 * @Title: to_check
	 * @Description: 审核界面
	 * @author 张庆奇
	 * @param id
	 * @return
	 * @return HtmlRestResult
	 * @throws
	 * @date 2016年8月5日 上午10:26:26
	 */
	@RequestMapping(value = "/to_check")
	public HtmlRestResult to_check(String id) {
		ModelResult<InvestorWithdrawRecordEntity> recordModel = investorWalletService.queryWithdrawRecordById(id);
		InvestorWithdrawRecordEntity record = new InvestorWithdrawRecordEntity();
		if (recordModel.isSuccess()) {
			record = recordModel.getModel();
		}
		if (record != null && StringUtil.isNotEmpty(record.getInvestorId())) {
			ModelResult<InvestorEntity> investorModel = investorService.queryInvestorById(record.getInvestorId());
			InvestorEntity investor = new InvestorEntity();
			if (investorModel.isSuccess()) {
				investor = investorModel.getModel();
			}
			ViewContext.put("investor", investor);
		}
		ViewContext.put("record", record);
		ViewContext.put("status", 0);
		BaseRestResult.refresh();
		return HtmlRestResult.popHtmlFragment("/withdraw/check");
		
	}
	
	@RequestMapping(value="/check")
	public  BaseRestResult<String> check(int status,String id,HttpServletRequest request) {
		BaseRestResult<String> result = null;
		String url="/withdraw/index" ;
		InvestorWithdrawRecordEntity entity=new InvestorWithdrawRecordEntity() ;
		HttpSession session =request.getSession();
		AdminUserEntity userName=(AdminUserEntity) session.getAttribute("adminUser");
		entity.setId(id);
		if(status==0){
			entity.setStatus(2);//不通过
		}else{
			entity.setStatus(3);//通过
		}
		entity.setUpdateTime(new Date());
		entity.setVerifier(userName!=null?userName.getId():"");
		ModelResult<Boolean> flag_model=investorWalletService.updateWithdrawRecord(entity);
		boolean flag = false;
		if(flag_model.isSuccess()){
			flag = flag_model.getModel();
		}
		
		if (flag) {
			result = BaseRestResult.warn("操作成功").andRedirect(url);
		} else {
			result = BaseRestResult.error("操作失败");
		}
		
		return result;
		
	}
	

}
